package org.wen.soft.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

@Component
public class PropUtil {

    static String projectDir;
    static String projectPath;
    static String modelPaths;
    static String javaPath;
    static String javaTestPath;
    static String classPath;
    static String classTestPath;
    static String generateDir;

    private static final String separator = File.separator;

    public static final String template = "PowerMock.java";


    public @Value("${generate.projectPath}") void setProjectPath(String projectPath) {
        PropUtil.projectPath = projectPath;
    }

    public @Value("${generate.javaPath}") void setJavaPath(String javaPath) {
        PropUtil.javaPath = javaPath;
    }

    public @Value("${generate.projectDir}") void setProjectDir(String projectDir) {
        PropUtil.projectDir = projectDir;
    }

    public @Value("${generate.modelPaths}") void setModelPaths(String modelPaths) {
        PropUtil.modelPaths = modelPaths;
    }

    public @Value("${generate.javaTestPath}") void setJavaTestPath(String javaTestPath) {
        PropUtil.javaTestPath = javaTestPath;
    }

    public @Value("${generate.classPath}") void setClassPath(String classPath) {
        PropUtil.classPath = classPath;
    }

    public @Value("${generate.classTestPath}") void setClassTestPath(String classTestPath) {
        PropUtil.classTestPath = classTestPath;
    }

    public @Value("${generate.generateDir}") void setGenerateDir(String generateDir) {
        PropUtil.generateDir = generateDir;
    }

    public static String getProjectDir() {
        return StrCastUtil.pathToPath(projectDir + separator + projectPath + separator);
    }

    public static String getModelDir(String modelPath){
        if (!modelPaths.contains(modelPath)) {
            modelPath = "";
        }
        return StrCastUtil.pathToPath(getProjectDir() + modelPath + separator);
    }

    public static String getJavaDir(String modelPath) {
        return getModelDir(modelPath) + javaPath + separator;
    }
    public static String getJavaTestDir(String modelPath) {
        return getModelDir(modelPath) + javaTestPath + separator;
    }

    public static String getClassDir(String modelPath) {
        return getModelDir(modelPath) + classPath + separator;
    }

    public static String getClassTestDir(String modelPath) {
        return getModelDir(modelPath) + classTestPath + separator;
    }
    public static String getGenerateDir(String modelPath) {
        String projectModelPath = projectPath + separator + modelPath;
        if (modelPath.equals("unKnow")) {
            projectModelPath = "unKnow";
        }
        return generateDir + separator + projectModelPath + separator;
    }

    public static Set<String> getModelPaths() {
        Set<String> set = new HashSet<>();
        for (String path : modelPaths.split(",")) {
            set.add(path.trim());
        }
        // 如果modelPaths为空，则set将存在一个空文本，此时表示没有模块层级，恰好直接使用""作为目录
        return set;
    }

    public static String getModesNameByClassFile(String classFile) {
        String regex = classPath.replace("\\", "\\\\");
        String modelDir = classFile.split(regex)[0];
        String projectDir = getProjectDir();
        if (modelDir.contains(projectDir)) {
            return modelDir.replace(getProjectDir(), "");
        }
        return "unKnow";
    }
}
